package com.sh.controller;

import com.sh.pojo.Category;
import com.sh.pojo.Result;
import com.sh.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public Result add(@RequestBody Category category) {
        categoryService.add( category);
        return Result.success();
    }
    @GetMapping
    public Result list() {
        List<Category>list= categoryService.list();
        return Result.success(list);
    }
    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Category category= categoryService.detail(id);
        return Result.success(category);
    }
    @PutMapping
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        if (categoryService.count(id) > 0) {
            return Result.error("该分类下有文章，不能删除");
        }
        categoryService.delete(id);
        return Result.success();
    }
}
